package com.dai.nettyserve.utils;

/**
 * @author Administrator
 * @date 2021-06-07  10:49
 * 16进制跟ASCII相互转换的工具类
 */
public class HexAsciiUtil {


    /**
     * ASCII码hex字符串转String明文
     * 每两个字符表示的16进制ASCII码解析成一个明文字符
     * @param hex
     * @return
     */
    public static String hex2Str(String hex) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            String h = hex.substring(i, (i + 2));
            int decimal = Integer.parseInt(h, 16);
            sb.append((char) decimal);
        }
        return sb.toString();
    }

    /**
     * String明文转ASCII码hex字符串
     * 一个明文字符生成两个字符表示的16进制ASCII码
     * @param str
     * @return
     */
    public static String str2Hex(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            // 这里的第二个参数16表示十六进制
            sb.append(Integer.toString(c, 16));
        }
        return sb.toString();
    }


    public static void main(String[] args) {

        Long startTime = System.currentTimeMillis();

        //                          303039304332434132304542
//        System.out.println(hex2Str("303039304332434132304542"));       // 0090C2CA20EB
//        System.out.println(hex2Str("334335323832453437324245"));      //3C5282E472BE


        System.out.println(  HexAsciiUtil.hex2Str("41 35 30 30 32 46 35 30 46 36 33 30 33 30 33 39 33 30 34 33 33 32 34 33 34 31 33 32 33 30 34 35 34 32 33 31 33 31 33 31 33 31 33 31 33 31 33 31 33 31 33 31 33 31 33 31 33 31 44 43 43 44 30 31 41 41 41 41 41 31 30 30 31 31 45 31 30 31 35 35 30 30 30 30 30 30 30 30 43 44 44 43 61 31".replace(" ","")));

        Long endTime = System.currentTimeMillis();

        // 计算并打印耗时
        Long tempTime = (endTime - startTime);
        System.out.println("花费时间："+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
    }

}
